<template>
    <div>
    <div class="oneBox">
        <el-form
    ref="ruleFormRef"
    style="width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="账号" prop="username">
      <el-input v-model="ruleForm.username" />
    </el-form-item>
    <el-form-item label="密码" prop="proword">
      <el-input v-model="ruleForm.proword" />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <el-input v-model="ruleForm.code" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="disabled=true">
        手机号登录
      </el-button>
    </el-form-item>
    </el-form>
    
    </div>
    <div class="twoBox">
        <el-form
    ref="ruleFormRef"
    style="width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="手机号" prop="username">
      <el-input v-model="ruleForm.phonename" />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <el-input v-model="ruleForm.phonecode" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary">
        登录
      </el-button>
    </el-form-item>
    </el-form>
    
    </div>
    </div>
</template>
<script setup>
import { reactive, ref } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';

const ruleFormRef = ref()
const ruleForm = reactive({})
const disabled=ref(false)
const router=useRouter()




const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('输入11位的手机号'))
  } else {
    if (Array(value).length !== 11) {
        callback(new Error('输入11位的手机号,开头必须是1'))
    }
    callback()
  }
}

const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  proword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  phonename: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  phonecode: [
    { required: true, message: '请输入短信验证码', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },

  ],
})



const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
    router.push('/home')
      console.log('submit!')
      request.post('/login',ruleForm).then(res=>{
        if(res.status==200&&res.data.data.code==10000){
            ElMessage.success(res.data.data.message)
        }else{
            ElMessage.error('账号或密码验证码错误')
        }
      })
     
    } else {
      console.log('error submit!', fields)
      ElMessage.error('请输入完整信息')
    }
  })
}

// const submitFormphone = async (formEl) => {
//   if (!formEl) return
//   await formEl.validate((valid, fields) => {
//     if (valid) {
//       console.log('submit!')
//       request.post('/login',ruleForm).then(res=>{
//         if(res.status==200&&res.data.data.code==10000){
//             ElMessage.success(res.data.data.message)
//         }else{
//             ElMessage.error('账号或密码验证码错误')
//         }
//       })
//       router.push('/home')
//     } else {
//       console.log('error submit!', fields)
//       ElMessage.error('请输入完整信息')
//     }
//   })
// }
</script>